package com.example.smallandroidcodetester;

import java.util.ArrayList;
import java.util.HashSet;

import android.app.ActionBar;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

public class ListMultiChoice extends SherlockActivity {

	private ListView listView;
	private ArrayList<String> examples = new ArrayList<String>();
	private LayoutInflater inflater;
	private ActionBar actionBar;
	private ActionMode actionMode;
	private MultiChoiceListAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_multi_choice);

		actionBar = getActionBar();
		actionBar.setTitle("TESTER");
		actionBar.setSubtitle("locsics");

		inflater = (LayoutInflater) LayoutInflater.from(ListMultiChoice.this);

		// listView = (ListView) findViewById(R.id.ListMultiChoice);
		adapter = new MultiChoiceListAdapter();
		listView.setAdapter(adapter);
		for (int i = 0; i < 10; i++) {
			examples.add("alma");
		}
		listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
				listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
				startActionMode(new ActionModeCallback());
				if (actionMode != null) {
					// actionMode.invalidate();
				}
				return true;
			}

		});
	}

	private class MultiChoiceListAdapter extends BaseAdapter {

		private boolean multiMode = false;
		// all our checked indexes go here
		private HashSet<Integer> checkedItems;

		@Override
		public int getCount() {
			return examples.size();
		}

		@Override
		public Object getItem(int position) {
			return examples.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = inflater.inflate(R.layout.list_multi_choice_item, null);
			TextView tv = (TextView) v.findViewById(R.id.ListItemTitle);
			tv.setText(examples.get(position));
			return v;
		}

		public void enterMultiMode() {
			this.multiMode = true;
			notifyDataSetChanged();
		}

		public void exitMultiMode() {
			checkedItems.clear();
			this.multiMode = false;
			notifyDataSetChanged();
		}
	}

	private final class ActionModeCallback implements ActionMode.Callback {

		@Override
		public boolean onCreateActionMode(ActionMode mode, Menu menu) {
			System.out.println("ListMultiChoice.ActionModeCallback.onCreateActionMode()");
			adapter.enterMultiMode();
			// update title with number of checked items
			mode.setTitle("IN MULTI MODE");
			actionMode = mode;
			return false;
		}

		@Override
		public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
			System.out.println("ListMultiChoice.ActionModeCallback.onPrepareActionMode()");
			menu.clear();
			getSupportMenuInflater().inflate(R.menu.cab_menu, menu);
			return false;
		}

		@Override
		public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
			System.out.println("ListMultiChoice.ActionModeCallback.onActionItemClicked()");
			return false;
		}

		@Override
		public void onDestroyActionMode(ActionMode mode) {
			System.out.println("ListMultiChoice.ActionModeCallback.onDestroyActionMode()");
			adapter.exitMultiMode();
			// don't forget to remove it, because we are assuming that if it's not null we are in ActionMode
			actionMode = null;
		}

	}
}
